package com.tcoiss.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tcoiss.app.domain.ExcelVo;
import com.tcoiss.app.domain.GaodeDz;
import com.tcoiss.app.service.IGaodeDzService;
import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.common.component.api.RemoteApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 高德编码对照表Controller
 * 
 * @author zw
 * @date 2021-08-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/gaodeDz" )
public class GaodeDzController extends BaseController {

    private final IGaodeDzService iGaodeDzService;

    private final RemoteApiService remoteApiService;

    /**
     * 查询高德编码对照表列表
     */
    @PreAuthorize(hasPermi = "system:dz:list")
    @GetMapping("/list")
    public TableDataInfo list(GaodeDz gaodeDz) {
        startPage();
        List<GaodeDz> list = iGaodeDzService.queryList(gaodeDz);
        return getDataTable(list);
    }

    /**
     * 导出高德编码对照表列表
     */
    @PreAuthorize(hasPermi = "system:dz:export" )
    @Log(title = "高德编码对照表" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,GaodeDz gaodeDz) throws IOException
    {
        List<GaodeDz> list = iGaodeDzService.queryList(gaodeDz);
        ExcelUtil<GaodeDz> util = new ExcelUtil<GaodeDz>(GaodeDz.class);
        util.exportExcel(response,list, "dz" );
    }

    /**
     * 获取高德编码对照表详细信息
     */
    @PreAuthorize(hasPermi = "system:dz:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGaodeDzService.getById(id));
    }

    /**
     * 新增高德编码对照表
     */
    @PreAuthorize(hasPermi = "system:dz:add" )
    @Log(title = "高德编码对照表" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GaodeDz gaodeDz) {
        return toAjax(iGaodeDzService.save(gaodeDz) ? 1 : 0);
    }

    /**
     * 修改高德编码对照表
     */
    @PreAuthorize(hasPermi = "system:dz:edit" )
    @Log(title = "高德编码对照表" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GaodeDz gaodeDz) {
        return toAjax(iGaodeDzService.updateById(gaodeDz) ? 1 : 0);
    }

    /**
     * 删除高德编码对照表
     */
    @PreAuthorize(hasPermi = "system:dz:remove" )
    @Log(title = "高德编码对照表" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGaodeDzService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 手工同步对照表
     */
    /*@Log(title = "手工同步对照表" , businessType = BusinessType.SYNC)
    @RequestMapping("/handleSync" )
    public AjaxResult handleSync() {
        return toAjax(iGaodeDzService.syncDzBill() ? 1 : 0);
    }*/

    /**
     * 高德数据库查询高德API
     */
    @Log(title = "数据库查询高德API" , businessType = BusinessType.SYNC)
    @RequestMapping("/dbQueryApi" )
    public AjaxResult dbQueryApi() {
        return toAjax(iGaodeDzService.dbQueryApi() ? 1 : 0);
    }

    @Log(title = "拉取高德编码表", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<ExcelVo> townCode = iGaodeDzService.getTownCode();
        ExcelUtil<ExcelVo> util = new ExcelUtil<ExcelVo>(ExcelVo.class);
        util.exportExcel(response, townCode,"苍穹编码表");
    }

    @Log(title = "手工对照" , businessType = BusinessType.SYNC)
    @GetMapping("/handleDZ" )
    public AjaxResult handleDZ() {
        return toAjax(iGaodeDzService.handleDZ() ? 1 : 0);
    }

    @Log(title = "发送对照消息" , businessType = BusinessType.SYNC)
    @RequestMapping("/sendDzMsg" )
    public AjaxResult sendDzMsg() {
        LambdaQueryWrapper<GaodeDz> lqw = Wrappers.lambdaQuery();
        lqw.in(GaodeDz::getStatus,new String[]{"2"});
        List<GaodeDz> gaodeDzs1 = iGaodeDzService.list(lqw);
        LambdaQueryWrapper<GaodeDz> lqw2 = Wrappers.lambdaQuery();
        lqw2.in(GaodeDz::getStatus,new String[]{"3"});
        List<GaodeDz> gaodeDzs2 = iGaodeDzService.list(lqw2);
        Map<String,Object> message1 = new HashMap<>();
        List<Map<String,Object>> articles1 = new ArrayList<>();
        Map<String,Object> article = new HashMap<>();
        String erroqh = "苍穹异常区划：";
        for(GaodeDz gaodeDz:gaodeDzs1){//苍穹对照高德异常
            erroqh = erroqh+"|"+gaodeDz.getLocalName();
        }
        String description1 = erroqh + "\n"+"异常数："+ gaodeDzs1.size() + "\n";
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        description1 = description1+"对照日期："+ sdf.format(gaodeDzs1.get(0).getCreateTime()) + "\n" +
                "原因分析：名称改变或高德删除" + "\n"+
                "账号，密码：admin/admin123";
        article.put("title","苍穹对照高德异常");
        article.put("description",description1);
        article.put("url","http://124.71.130.38/index#/system/adminDivision/gaodeDz");
        articles1.add(article);
        message1.put("jobNo","1107074");
        message1.put("articles",articles1);
        AjaxResult ajaxResult = remoteApiService.sendWxMessage(message1);
        int code = -1;
        if(Integer.valueOf(ajaxResult.get("code").toString())==200){
            code = Integer.valueOf(ajaxResult.get("data").toString());
        }

        if(code==0){
            List<Map<String,Object>> articles2 = new ArrayList<>();
            Map<String,Object> message2 = new HashMap<>();
            Map<String,Object> article1 = new HashMap<>();
            String erroqh2 = "高德异常区划：";
            for(GaodeDz gaodeDz:gaodeDzs2){//高德对照苍穹异常
                erroqh2 = erroqh2+"|"+gaodeDz.getGaodeName();
            }

            String description2 = erroqh2 + "\n"+"异常数："+ gaodeDzs2.size() + "\n";
            description2 = description2+"对照日期："+ sdf.format(gaodeDzs1.get(0).getCreateTime()) + "\n" +
                    "原因分析：名称改变或高德增加" + "\n"+
                    "账号，密码：admin/admin123";
            article1.put("title","高德对照苍穹异常");
            article1.put("description",description2);
            article1.put("url","http://124.71.130.38/index#/system/adminDivision/admindivision");

            articles2.add(article1);
            message2.put("jobNo","1107074");
            message2.put("articles",articles2);
            AjaxResult ajaxResult2 = remoteApiService.sendWxMessage(message1);
            int code2 = -1;
            if(Integer.valueOf(ajaxResult2.get("code").toString())==200){
                code2 = Integer.valueOf(ajaxResult2.get("data").toString());
            }
            if(code2==0){
                return AjaxResult.success();
            }
            return AjaxResult.error("消息推送失败");
        }
        return AjaxResult.error("消息推送失败");

    }

}
